package com.jinlia.show.modules.wxapp.controller;

import com.jinlia.show.common.core.utils.UserInfoUtils;
import com.jinlia.show.modules.base.constant.EnableStatusEnum;
import com.jinlia.show.modules.base.constant.FissionTypeEnum;
import com.jinlia.show.modules.base.wxapp.entity.Fission;
import com.jinlia.show.modules.base.wxapp.service.FissionMapperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 裂变
 */
@Slf4j
@RestController
@RequestMapping("fission")
public class FissionController {

    @Autowired
    private FissionMapperService fissionMapperService;

    @PostMapping("new")
    public Fission newFission(@RequestBody Fission fission) {
        if (fission.getParentOpenid() == null) {
            log.info("裂变没有父id");
            return fission;
        }
        if (fission.getFissionType() == null) {
            fission.setFissionType(FissionTypeEnum.REGISTER.getValue());
        }
        fission.setEnableStatus(EnableStatusEnum.FISSION_SUCCESS.getValue());
        fission.setChildOpenid(UserInfoUtils.getOpenid());
        fission = fissionMapperService.newFission(fission);
        return fission;
    }
    @PostMapping("my")
    public List<Fission> myFission(@RequestBody Fission fission) {
        if (fission.getParentOpenid() == null && fission.getChildOpenid() == null) {
            fission.setParentOpenid(UserInfoUtils.getOpenid());
        }
        return fissionMapperService.myFission(fission);
    }
}
